package ui.label;

import controllers.ComboButtonControl;
import ui.common.CommonComboBoxPanel;
import ui.panel.WorkPanel;
import utils.CommonUtil;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class ComboLabel extends JLabel {

    private CommonComboBoxPanel commonComboBoxPanel;
    private WorkPanel workPanel;
    private JLabel valueLabel = new JLabel();
    private JButton choose = new JButton();
    public String value;
    public int i;
    public int model = 0;

    public ComboLabel(int y, int width, String value, WorkPanel workPanel) throws IOException {
        this.value = value;
        this.workPanel = workPanel;
        this.model = 0;
        setParamLabel(y, width);
    }

    public ComboLabel(int i, int y, int width, String value, CommonComboBoxPanel commonComboBoxPanel) throws IOException {
        this.i = i;
        this.value = value;
        this.commonComboBoxPanel = commonComboBoxPanel;
        this.model = 1;
        setParamLabel(y, width);
    }

    private void setParamLabel(int y, int width) throws IOException {

        Font font = CommonUtil.getFont();
        this.setIcon(ImageUtils.common);
        this.setVerticalTextPosition(JLabel.CENTER);
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setFont(font);
        this.setForeground(Color.WHITE);
        this.setBounds(0,y,width,30);
        valueLabel.setText(value);
        valueLabel.setFont(font);
        valueLabel.setForeground(Color.WHITE);
        valueLabel.setBounds(15,0,width,30);
        valueLabel.setOpaque(false);
        //初始化选择按钮
        choose.setContentAreaFilled(false);
        choose.setName("" + value);
        choose.setBorder(null);
        choose.setBounds(0,0, width,30);
        choose.addActionListener(new ComboLabel.chooseListener());
        //添加鼠标事件
        ComboButtonControl mouse = new ComboButtonControl(choose);
        choose.addMouseListener(mouse);

        this.add(choose);
        this.add(valueLabel);
        this.add(new BarLabel(5,1));
//        this.add(bg);

    }

    private class chooseListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (model == 0){
                workPanel.setName(value);
            }
            if (model == 1){
                commonComboBoxPanel.closeComboList(i, value);
            }
        }
    }

}

